import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.linalg.distributed.RowMatrix

object SVD {
  val conf = new SparkConf()                                     //创建环境变量
    .setMaster("local")                                             //设置本地化处理
    .setAppName("SVD")                              //设定名称
  val sc = new SparkContext(conf)

  def main(args: Array[String]) {
    val data = sc.textFile("/Users/unclewang/Idea_Projects/cloudera/src/main/resources/a.txt")
      .map(_.split(" ").map(_.toDouble))
      .map(line => Vectors.dense(line))

    val rm = new RowMatrix(data)                       //读入行矩阵
    val SVD = rm.computeSVD(2, computeU = true)			 //进行SVD计算
    println(SVD)			 //打印SVD结果矩阵
    //求　SVD 分解的矩阵
    println("*********************")
    val u = SVD.U
    val s = SVD.s
    val v = SVD.V

    println(u, s, v)
  }
}
